package com.sportswork.sportswork.controller.admin;

import com.sportswork.sportswork.config.SecurityUserInfo;
import com.sportswork.sportswork.core.entity.Classroom;
import com.sportswork.sportswork.core.entity.Credit;
import com.sportswork.sportswork.core.service.dto.PageDTO;
import com.sportswork.sportswork.core.service.impl.ClassServiceImp;
import com.sportswork.sportswork.core.service.impl.CreditServiceImp;
import com.sportswork.sportswork.core.service.impl.TeacherServiceImp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @author dengwei
 * @date 2020/2/3 12:11
 * @description
 */
@Slf4j
@Controller("ClassController")
public class ClassController {
    @Resource
    private ClassServiceImp classServiceImp;

    @RequestMapping("/admin/classroom/list")
    public String List(){
        return "/pages/view/admin/classroom/list";
    }

    @RequestMapping("/admin/classroom/getAllClassroom")
    @ResponseBody
    public Object getAllClassroom(String number,String name){
        SecurityUserInfo userDetails = (SecurityUserInfo) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        String xsid = "";
        if(userDetails.getRoleId().equals("3")){
            xsid = userDetails.getUserId();
        }
        return new  PageDTO<Classroom>().toPageDTO(classServiceImp.getAllClassrooms(number,name,xsid));
    }

    @RequestMapping(value = "/admin/classroom/add", method = RequestMethod.POST)
    @ResponseBody
    public String add(Classroom classroom) {
        classServiceImp.addClassroom(classroom);
        return "success";
    }

    @RequestMapping(value = "/admin/classroom/edit", method = RequestMethod.POST)
    @ResponseBody
    public String edit(Classroom classroom) {
        classServiceImp.setClassroom(classroom);
        return "success";
    }

    @RequestMapping(value = "/admin/classroom/delete", method = RequestMethod.POST)
    @ResponseBody
    public String delete(@RequestParam String id) {
        classServiceImp.deleteClassroom(id);
        return "true";
    }

    @RequestMapping(value = "/admin/classroom/deletes", method = RequestMethod.POST)
    @ResponseBody
    public String deletes(@RequestParam String ids) {
        classServiceImp.deleteClassrooms(ids );
        return "true";
    }
}
